package cn.com.shopec.erp.base.controller;
import java.util.List;
import javax.annotation.Resource;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSONObject;
import cn.com.shopec.erp.base.entity.LogisticsInfo;
import cn.com.shopec.erp.base.entity.LogisticsSettle;
import cn.com.shopec.erp.base.service.ILogisticInfoService;
import cn.com.shopec.erp.base.service.ILogisticSettleService;
import cn.com.shopec.erp.common.constant.SZSCMConstant;
import cn.com.shopec.erp.common.page.PageFinder;
import cn.com.shopec.erp.common.page.PageQuery;
import cn.com.shopec.erp.common.utils.JsonHelper2;
import cn.com.shopec.erp.common.utils.LogicUtil;
import cn.com.shopec.erp.common.utils.StringUtil;

/**
 * 物流管理
 * 
 * @author Administrator
 */
@Controller
@RequestMapping("base/logisticsManageController")
public class LogisticsManageController
{
     /** 日志. */
     private Logger log = Logger.getLogger(LogisticsManageController.class);
     @Resource
     private ILogisticInfoService logisticInfoService;
     @Resource
     private ILogisticSettleService logisticSettleService;
     
     /**
      * 分页查询物流信息
      * 
      * @param logisticsInfo
      * @param pageQuery
      * @param map
      * @return
      */
     @RequestMapping("toPageFinderLogisticInfo")
     public String pageFinderLogisticInfo(LogisticsInfo logisticsInfo, PageQuery pageQuery, ModelMap map)
     {
          PageFinder<LogisticsInfo> pageFinder = this.logisticInfoService.getPageFinderLogisticInfo(logisticsInfo, pageQuery);
          String json = JsonHelper2.toJson(pageFinder.getData());
          map.put("rows", json);
          map.put("total", pageFinder.getData().size());
          map.put("LogisticsInfo", logisticsInfo);
          return "/base/logistic_info_list";
     }
     
     
     
     /**
      * 分页查询物流信息
      * 
      * @param logisticsInfo
      * @param pageQuery
      * @param map
      * @return
      */
     @RequestMapping("ajaxPageFinderLogisticInfo")
     @ResponseBody
     public String ajaxPageFinderLogisticInfo(LogisticsInfo logisticsInfo, PageQuery pageQuery, ModelMap map)
     {
    	  JSONObject jsonObj = new JSONObject();
          PageFinder<LogisticsInfo> pageFinder = this.logisticInfoService.getPageFinderLogisticInfo(logisticsInfo, pageQuery);
          jsonObj.put("Rows", pageFinder.getData());
          jsonObj.put("Total", pageFinder.getRowCount());
          return jsonObj.toJSONString();
     }
     
     
     
     /**
      * 跳转到设置页面
      * 
      * @param code
      * @param settleType
      * @return
      */
     @RequestMapping("toSetAgency")
     public String toSetAgency(String code, String settleType, ModelMap map)
     {
          List<LogisticsSettle> setList = logisticSettleService.getLogisticsSettleByCode(code, settleType);
          LogisticsSettle setInfo = null;
          if (null != setList && setList.size() > 0)
          {
               setInfo = setList.get(0);
          }
          map.put("code", code);
          map.put("settleType", settleType);
          map.put("LogisticsSettle", setInfo);
          return "/base/set_agency";
     }
     
     /**
      * 跳转到手续费设置页面
      * 
      * @param code
      * @param settleType
      * @return
      */
     @RequestMapping("toSetPoundage")
     public String toSetPoundage(String code, String settleType, ModelMap map)
     {
          List<LogisticsSettle> setList = logisticSettleService.getLogisticsSettleByCode(code, settleType);
          LogisticsSettle setInfo = null;
          if (null != setList && setList.size() > 0)
          {
               setInfo = setList.get(0);
          }
          map.put("code", code);
          map.put("settleType", settleType);
          map.put("LogisticsSettle", setInfo);
          return "/base/set_poundage";
     }
     
     /**
      * 设置代收款、运费
      * 
      * @param logisticsSettle
      * @return
      * @throws Exception
      */
     @RequestMapping("setAgencyAndFare")
     @ResponseBody
     public String setAgencyAndFare(LogisticsSettle logisticsSettle) throws Exception
     {
          if (null != logisticsSettle)
          {
               try
               {
                    logisticSettleService.setLogisticsSettleInfo(logisticsSettle);
                    return SZSCMConstant.SUCCESS;
               }
               catch (Exception e)
               {
                    log.info("setAgencyAndFare" + e.getMessage(), e);
               }
          }
          return SZSCMConstant.FAIL;
     }
     
     
     
     /**
      * 跳转到新增物流页面
      * 
      * @return
      */
     @RequestMapping("toAddLogisticInfo")
     public String toAddLogisticInfo()
     {
          return "/base/add_logistic";
     }
     
     
     /**
      * 新增物流信息
      * 
      * @param logisticsInfo
      * @return
      * @throws Exception
      */
     @RequestMapping("c_logisticInfo")
     @ResponseBody
     public String c_logisticInfo(LogisticsInfo logisticsInfo) throws Exception
     {
          if (null != logisticsInfo)
          {
               try
               {
                    logisticInfoService.addLogisticInfo(logisticsInfo);
                    return SZSCMConstant.SUCCESS;
               }
               catch (Exception e)
               {
                    log.info("c_logisticInfo" + e.getMessage(), e);
               }
          }
          return SZSCMConstant.FAIL;
     }
     
     /**
      * 跳转修改物流信息
      * 
      * @param code
      * @param map
      * @return
      */
     @RequestMapping("toUpdateLogisticInfo")
     public String toUpdateLogisticInfo(String code, ModelMap map)
     {
          map.put("logisticsInfo", logisticInfoService.getLogisticsInfoByCode(code));
          return "/base/update_logistic";
     }
     
     
     /**
      * 修改物流信息
      * 
      * @param logisticsInfo
      * @return
      * @throws Exception
      */
     @RequestMapping("u_logisticInfo")
     @ResponseBody
     public String u_logisticInfo(LogisticsInfo logisticsInfo) throws Exception
     {
          if (null != logisticsInfo)
          {
               try
               {
                    logisticInfoService.updateLogisticsInfo(logisticsInfo);
                    return SZSCMConstant.SUCCESS;
               }
               catch (Exception e)
               {
                    log.info("u_logisticInfo" + e.getMessage(), e);
               }
          }
          return SZSCMConstant.FAIL;
     }
     
     
     /**
      * 物流明细
      * 
      * @param code
      * @param map
      * @return
      */
     @RequestMapping("detailLogisticInfo")
     public String detailLogisticInfo(String code, ModelMap map)
     {
          map.put("logisticsInfo", logisticInfoService.getLogisticsInfoByCode(code));
          map.put("logisticList", logisticSettleService.getAllLogisticsSettleByCode(code));
          return "/base/detail_logistic";
     }
     
     
     /**
      * 删除物流
      * 
      * @param code
      * @return
      * @throws Exception
      */
     @RequestMapping("d_logisticInfo")
     @ResponseBody
     public String d_logisticInfo(String code) throws Exception
     {
          if (LogicUtil.isNotNullAndEmpty(code))
          {
               try
               {
                    logisticInfoService.delLogisticInfo(code);
                    return SZSCMConstant.SUCCESS;
               }
               catch (Exception e)
               {
                    log.info("d_logisticInfo" + e.getMessage(), e);
               }
          }
          return SZSCMConstant.FAIL;
     }
     
     
     /**
      * 验证物流编码唯一性
      * @param logisticId
      * @return
      */
     @RequestMapping("checkLogisticId")
     @ResponseBody
     public String checkLogisticId(String logisticsId){
    	 if(StringUtil.isExist(logisticsId)){
    		 List<LogisticsInfo> list = logisticInfoService.getLogisticInfoche(logisticsId);
    		 if(null != list && list.size()>0){
    			 return SZSCMConstant.FAIL;
    		 }
    	 }
		return SZSCMConstant.SUCCESS;
     }
     
     
     /**
      * 验证物流名字唯一性
      * @param logisticId
      * @return
      */
     @RequestMapping("checkLogisticName")
     @ResponseBody
     public String checkLogisticName(String logisticName){
    	 if(StringUtil.isExist(logisticName)){
    		 List<LogisticsInfo> list = logisticInfoService.getLogisticsByName(logisticName);
    		 if(null != list && list.size()>0){
    			 return SZSCMConstant.FAIL;
    		 }
    	 }
		return SZSCMConstant.SUCCESS;
     }
     
}
